<%--
  Created by IntelliJ IDEA.
  User: 27716
  Date: 2024/10/29
  Time: 15:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改</title>
    <script src="../../static/js/jquery-1.8.2.min.js"></script>
    <link rel="stylesheet" href="../../static/css/css.css">
    <script>
        $(function (){
            //全局
            $.ajaxSettings.async = false;
            //先下拉 后回显
            //动态下拉框
            getGoodsSelect();
            getSupplierSelect();
            //回显
            findById();
        })

            //商品类型 查询所有数据
            function getGoodsSelect(){
                $.ajax({
                    url:"/goodsType/goodsType",
                    dataType:"json",
                    success(res){
                        console.log(res);
                        for (let o of res) {
                            //追加数据
                            $("#goodsTypeId").append(`
                            <option value="\${o.goodsTypeId}">\${o.goodsTypeName}</option>
                    `)
                        }
                    },
                    error(){
                        alert("服务器故障")
                    }
                })
            }
            function getSupplierSelect(){
                $.ajax({
                    url:"/supplier/getSupplier",
                    dataType: "json",
                    success(res) {
                        for (let o of res) {
                            $("#supplierId").append(`
                            <option value="\${o.supplierId}">\${o.supplierName}</option>
                        `)
                        }
                    },
                    error() {
                        alert("服务器出现问题")
                    }
                })
            }

        function findById(){
            let goodsId = $("#goodsId").val();
            let obj = {goodsId:goodsId};
            $.ajax({
                url:"/goodschange/findById",
                type:"post",
                data:{reqInfos:JSON.stringify(obj)},
                dataType:"json",
                success(res){
                    console.log(res);
                    $("#goodsCode").val(res.goodsCode);
                    $("#goodsName").val(res.goodsName);
                    $("#inventoryQuantity").val(res.inventoryQuantity);
                    //下拉框回显
                    $("#goodsTypeId").val(res.goodsTypeId);
                    $("#supplierId").val(res.supplierId);
                },
                error(){
                    alert("服务器错误");
                }
            })
        }
    </script>
</head>
<body>
<input type="hidden" value="${param.goodsId}" id="goodsId">
商品编码： <input type="text" id="goodsCode"> <br>
商品名称： <input type="text" id="goodsName"> <br>
库存数量： <input type="text" id="inventoryQuantity"> <br>
商品名：<select id="goodsTypeId">
    <option value="">请选择</option>
</select> <br>
供应商名称： <select  id="supplierId">
    <option value="">请选择</option>
</select>
<input type="button" value="提交" onclick="goUpd()">
<input type="button" value="返回" onclick="history.back()">
</body>
<script>
    function goUpd(){
        let goodsId = $("#goodsId").val();
        let goodsCode = $("#goodsCode").val();
        let goodsName = $("#goodsName").val();
        let inventoryQuantity = $("#inventoryQuantity").val();
        let goodsTypeId = $("#goodsTypeId").val();
        let supplierId = $("#supplierId").val();
        let obj = {
            goodsId:goodsId,
            goodsCode:goodsCode,
            goodsName:goodsName,
            inventoryQuantity:inventoryQuantity,
            goodsTypeId:goodsTypeId,
            supplierId:supplierId
        }
        $.ajax({
            url:"/goodschange/getUpd",
            type: "post",
            data: {reqInfos:JSON.stringify(obj)},
            dataType:"json",
            success(res){
                console.log(res);
                if(res === 200){
                    alert("修改成功");
                    location.href = "list.jsp";
                }
            },
            error(){
                alert("服务器错误")
            }
        })
    }
</script>
</html>
